<template>
  <div class="article-list">
    {{ channel.name }}
    <van-pull-refresh
      v-model="isLoading"
      :success-text="refreshText"
      :success-duration="1500"
      @refresh="onRefresh"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <!-- <van-cell
          v-for="article in list"
          :key="article.art_id"
          :title="article.title"
        /> -->
        <article-item
          v-for="(article, index) in list"
          :key="index"
          :article="article"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArticles } from '@/api/article'
import ArticleItem from '@/components/article-item'

export default {
  name: 'ArticleList',
  components: {
    ArticleItem,
  },
  props: {
    channel: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      list: [], // 存储列表数据的数组
      loading: false, // 控制加载中 loading 状态
      finished: false, // 控制数据加载结束的状态
      timestamp: null,
      error: false, // 是否加载失败
      isLoading: false,
      refreshText: '',
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onLoad() {
      try {
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(),
          with_top: 1,
        })
        console.log(data)
        const { results } = data.data
        this.list.push(...results)
        this.loading = false
        // results = []
        if (results.length === 0) {
          this.finished = true
        } else {
          this.timestamp = data.data.pre_timestamp
        }
      } catch (err) {
        console.log(err)
        this.loading = false // 关闭 loading 效果
        this.error = true // 开启错误提示
      }
    },
    async onRefresh() {
      try {
        const { data } = await getArticles({
          channel_id: this.channel.id,
          timestamp: Date.now(),
          with_top: 1,
        })
        console.log(data.data.results)
        const { results } = data.data
        this.list.unshift(...results)
        this.isLoading = false
        this.refreshText = `刷新成功 更新了${results.length}条数据`
      } catch (err) {
        this.isLoading = false
        this.refreshText = '刷新失败'
      }
    },
    // 初始化或滚动到底部的时候会触发调用 onLoad
    // onLoad() {
    //   console.log('onLoad')
    // 1. 请求获取数据
    // setTimeout 仅做示例，真实场景中一般为 ajax 请求
    // setTimeout(() => {
    // 2. 把请求结果数据追加放到 list 数组中  【一定要记住是追加，因为只有追加才会让列表高度不断增加】
    // for (let i = 0; i < 10; i++) {
    // 0 + 1 = 1
    // 1 + 1 = 2
    // 2 + 1 = 3
    // this.list.push(this.list.length + 1)
  },

  // 3. 本次数据加载结束之后要把加载状态设置为结束
  //     loading 关闭以后才能触发下一次的加载更多
  // this.loading = false

  // 4. 判断数据是否全部加载完成
  // if (this.list.length >= 40) {
  // 如果没有数据了，把 finished 设置为 true，之后不再触发加载更多
  // this.finished = true
  // }
  // }, 1000)
  // },
  // },
}
</script>

<style lang="less" scoped>
.article-item {
  .title {
    font-size: 32px;
    color: #3a3a3a;
  }

  .van-cell__title {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  .van-cell__value {
    flex: unset;
    width: 232px;
    height: 146px;
    padding-left: 25px;
  }

  .right-cover {
    width: 100%;
    height: 146px;
  }

  .label-info-wrap span {
    font-size: 22px;
    color: #b4b4b4;
    margin-right: 25px;
  }

  .cover-wrap {
    display: flex;
    padding: 30px 0;
    .cover-item {
      flex: 1;
      height: 146px;
      &:not(:last-child) {
        padding-right: 4px;
      }
      .cover-item-img {
        width: 100%;
        height: 146px;
      }
    }
  }
}
</style>
